package sz.szsmk.citizencard.ui.dzsmkavtivity;

import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.chenenyu.router.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.dzbean.res.MessageEvent;
import sz.szsmk.citizencard.mvp.BasePresenter;
import sz.szsmk.citizencard.mvp.GestureMvpView;
import sz.szsmk.citizencard.mvp.dzsmkpresenter.GesturePresenter;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.widget.GestureContentView;
import sz.szsmk.citizencard.widget.GestureDrawline;

import static sz.szsmk.citizencard.model.network.dzbean.res.MessageEvent.CODE_GESTURESUCCESS;

@Route("gesPasswordSettingActivity")
public class GesPasswordSettingActivity extends AppBaseActivity implements GestureMvpView {

    FrameLayout gestureContainer;
    TextView textTip;
    TextView title_right;

    private GestureContentView mGestureContentView;
    @Inject
    GesturePresenter gesturePresenter;
    private boolean mIsFirstInput = true;
    private String mFirstPassword = null;
    private String setType;
    boolean isNeedVaild;
    private String oldPwd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getComponent().inject(this);

        gestureContainer = findViewById(R.id.gesture_container);
        textTip = findViewById(R.id.text_tip);
        title_right = findViewById(R.id.iv_qr_right_txt);
        title_right.setOnClickListener(this::onClick);
        setType = getIntent().getStringExtra(Constant.TYPE);
        isNeedVaild = getIntent().getBooleanExtra("isNeedVaild",false);
        oldPwd = getIntent().getStringExtra("oldpwd");
        setContentView(R.layout.activity_ges_password_setting);
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true, 0f)
                .statusBarColor(R.color.white).init();
        mGestureContentView = new GestureContentView(this, false, "", new GestureDrawline.GestureCallBack() {
            @Override
            public void onGestureCodeInput(String inputCode) {
                if (!isInputPassValidate(inputCode)) {
                    textTip.setText(Html.fromHtml("<font color='#c70c1e'>至少连接4个点</font>"));
                    mGestureContentView.clearDrawlineState(0L);
                    return;
                }
                if (mIsFirstInput) {
                    mIsFirstInput = false;
                    textTip.setText(Html.fromHtml("<font color='#c70c1e'>请绘制确认手势</font>"));
                    mFirstPassword = inputCode;
                    mGestureContentView.clearDrawlineState(0);
                    title_right.setVisibility(View.VISIBLE);
                } else {
                    if (inputCode.equals(mFirstPassword)) {
                        //设置手势密码  接口
                        if(isNeedVaild){
                            if(TextUtils.isEmpty(oldPwd)){
                                gesturePresenter.resetGesturePassword(mFirstPassword);
                            }else{
                                gesturePresenter.modifyGesturePassword(oldPwd,mFirstPassword);
                            }
                        }else{
                            gesturePresenter.createGesturePassword(mFirstPassword,setType);
                        }

                    } else {
                        mIsFirstInput = false;
                        textTip.setText(Html.fromHtml("<font color='#c70c1e'>两次输入的手势密码不一致</font>"));
                        // 保持绘制的线，1.5秒后清除
                        mGestureContentView.clearDrawlineState(500L);
                    }
                }
            }

            @Override
            public void checkedSuccess() {

            }

            @Override
            public void checkedFail() {

            }
        });
        // 设置手势解锁显示到哪个布局里面
        mGestureContentView.setParentView(gestureContainer);
    }

    public void onClick(View v) {
        if (v.getId() == R.id.iv_qr_right_txt) {
            mIsFirstInput = true;
            textTip.setText(Html.fromHtml("<font color='#c70c1e'>请绘制解锁图案</font>"));
            mFirstPassword = "";
            title_right.setVisibility(View.GONE);
        }
    }

    @Override
    protected BasePresenter[] initPresenters() {
        return new BasePresenter[]{gesturePresenter};
    }

    @Override
    protected CharSequence getTopTitle() {
        return "设置手势密码";
    }

    private boolean isInputPassValidate(String inputPassword) {
        return !(TextUtils.isEmpty(inputPassword) || inputPassword.length() < 4);
    }

    @Override
    public void success() {
        Constant.isOpen = "1";
        MessageEvent event = new MessageEvent(CODE_GESTURESUCCESS);
        EventBus.getDefault().post(event);
        finish();
    }

    @Override
    public void fail() {

    }
}
